<template>
  <div class="container">
    <div class="query-form">
      <el-form ref="formRef" :model="queryForm" :rules="rules" inline>
        <el-form-item label="日期" prop="date">
          <el-date-picker
            v-model="queryForm.date"
            :clearable="false"
            type="date"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-select v-model="queryForm.status">
            <el-option label="待送达" value="待送达" />
            <el-option label="已完成" value="已完成" />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="handleQueryClick">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- <div>
        <div class="selectmuClockStyle">
          <el-select v-model="selectmuClock" style="width: 180px">
            <el-scrollbar height="100px">
            <template v-for="item in clockArrt">
              <el-option :label="item" :value="item" />
            </template>
             
            </el-scrollbar>
          </el-select>
        </div>
        <el-button type="primary" @click="handlePrintClick"> 打印 </el-button>
      </div> -->
    </div>
    <el-table
      v-loading="isLoading"
      :data="orders"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column prop="shopname" label="门店" width="180"></el-table-column>
      <el-table-column
        prop="peisongdianName"
        label="配送点"
        width="180"
      ></el-table-column>
      <el-table-column label="套餐名称">
        <template #default="scope">
          <el-table :data="scope.row.order_list" :show-header="false">
            <el-table-column prop="meal" label="套餐名称"></el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="已售数量">
        <template #default="scope">
          <el-table :data="scope.row.order_list" :show-header="false">
            <el-table-column prop="count" label="已售数量	"></el-table-column>
          </el-table>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import OrderResponse from "@/api/types/order/OrderResponse";
import { ElForm, ElMessage, FormRules } from "element-plus";
import { getMealOrderstatic } from "@/api/orderStatistics";
import { isAxiosError } from "axios";
import { formatDate } from "@/utils/date";

const queryForm = ref<{ date: string }>({
  date: formatDate(new Date()),
});
const formRef = ref<InstanceType<typeof ElForm>>();
const isLoading = ref(false);
const orders = ref<OrderResponse[]>([]);

const rules: FormRules = {
  date: [
    {
      required: true,
      message: "请选择日期",
      trigger: "change",
    },
  ],
};

const selectdataMune = ref<OrderResponse[]>([]);

const handleSelectionChange = (val: any) => {
  selectdataMune.value = val;
};

async function getData() {
  if (isLoading.value == false) {
    isLoading.value = true;
  }
  if (queryForm.value.date !== undefined) {
    try {
      const { result } = await getMealOrderstatic(queryForm.value.date);
      if (result !== undefined) {
        orders.value = result;
        isLoading.value = false;
      }
    } catch (error) {
      ElMessage.error("查询失败");
      isLoading.value = false;
    }
  }
}

onMounted(() => {
  handleQueryClick();
});

async function handleQueryClick() {
  formRef.value?.validate(async (isValid: boolean) => {
    if (isValid) {
      try {
        await getData();
      } catch (error) {
        if (isAxiosError(error) && error.response?.data.message != undefined) {
          ElMessage.error(error.response?.data.message);
        }
      }
    }
  });
}
</script>
<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;

  .query-form {
    display: flex;
    justify-content: space-between;
    padding-right: 30px;
    .el-select {
      --el-select-width: 240px;
    }
  }
}

#print-area {
  font-size: 16px;
  width: 320px;
  padding-left: 10px;
  display: none;
}

.selectmuClockStyle {
  display: inline-block;
  width: 200px;
}
</style>
